package com.action.admin.employee;

import java.util.Date;
import java.util.List;

import com.action.BaseAction;
import com.dao.EmployeeDao;
import com.dao.RolesDao;
import com.entity.Employee;
import com.util.Page;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;

/**
 * 
 * @author Administrator
 * 
 */
// @ParentPackage("json-default")
@ParentPackage("extendsjson-default")
@Namespace("/employee")
@ResultPath("/WEB-INF/pages")
public class EmployeeAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(EmployeeAction.class);

	/**
	 * 保存员工增加信息页
	 * 
	 * @return
	 */
	@Action(value = "addemp", results = { @Result(name = "success", location = "addemp.jsp"), })
	public String addemployee() {
		try {
			list = rolesDao.findRolelist();
			emp = employeeDao.findEmpById(id);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 保存员工增加信息页
	 * 
	 * @return
	 */
	@Action(value = "editemp", results = { @Result(name = "success", location = "editemp.jsp") })
	public String editemployee() {
		try {
			emp = employeeDao.findEmpById(id);
			list = rolesDao.findRolelist();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 检查员工是否存在
	 * 
	 * @return
	 */
	@Action(value = "checkemp", results = { @Result(name = "success", type = "json") })
	public String checkemployee() {
		try {
			flag = employeeDao.checkEmp(account);
			if (flag) {
				this.outJsonString("0");
			} else {
				this.outJsonString("1");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 保存员工信息
	 * 
	 * @return
	 */
	@Action(value = "saveemp", results = { @Result(name = "success", type = "json") })
	public String saveemployee() {
		try {
			emp.setCreatedate(new Date());
			flag = employeeDao.saveEmp(emp);
			if (flag) {
				this.outJsonString("0");
			} else {
				this.outJsonString("1");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 保存员工信息
	 * 
	 * @return
	 */
	@Action(value = "updateemp", results = { @Result(name = "success", type = "json") })
	public String updateemployee() {
		try {
			flag = employeeDao.updateEmp(emp);
			if (flag) {
				this.outJsonString("0");
			} else {
				this.outJsonString("1");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 *员工列表
	 * 
	 * @return
	 */
	@Action(value = "emplist", results = { @Result(name = "success", location = "employeelist.jsp") })
	public String employeelist() {
		try {
			page = employeeDao.emplist(page);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 *员工列表
	 * 
	 * @return
	 */
	@Action(value = "delemp", results = { @Result(name = "success", type = "json") })
	public String delemp() {
		try {
			flag = employeeDao.delEmpById(ids);
			if (flag)
				this.outJsonString("0");
			else
				this.outJsonString("1");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}

	private RolesDao rolesDao = (RolesDao) ioc.newInstance(RolesDao.class);
	private EmployeeDao employeeDao = (EmployeeDao) ioc.newInstance(EmployeeDao.class);
	private boolean flag = false;
	private Employee emp;
	private int id;
	private String ids;

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	private String stateus;
	private List list;
	private String account;
	private Page page = new Page();

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public String getStateus() {
		return stateus;
	}

	public void setStateus(String stateus) {
		this.stateus = stateus;
	}

	public Employee getEmp() {
		return emp;
	}

	public void setEmp(Employee emp) {
		this.emp = emp;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}
}
